using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Channels;
using System.Configuration;
using Microsoft.Win32;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;

namespace Microsoft.MSE.Catalog.Module {
    public static class Configurator {
        public static CatalogSvcConfig svcConfig;
        private static string svcConfigXml;

        public static void ReadConfig() {
            string regKey;
            regKey = SC.PlatformRegKey + @"\" + SC.ComponentName;

            svcConfig = ConfigurationManager.GetSection(SC.CatalogSvcConfigSettingName) as CatalogSvcConfig;

            if (svcConfig == null) {
                svcConfigXml = (string)Registry.GetValue(regKey, SC.CatalogSvcConfigSettingName, string.Empty);
                if (string.IsNullOrEmpty(svcConfigXml)) throw new ApplicationException(SC.CatalogSvcConfigSettingName + " not found in app.config nor in Registry - " + SC.ComponentName + " can not start");
                DataContractSerializer ser = new DataContractSerializer(typeof(CatalogSvcConfig));
                svcConfig = (CatalogSvcConfig)ser.ReadObject(XmlReader.Create(new StringReader(svcConfigXml)));
            }


        }
    }
}
